#include "mmu.h"


static bool mmu_inited = FALSE;
static uint32_t *tt_base = NULL; /* translaton table base */
static uint32_t *cp_base = NULL; /* coarse table base */
static uint32_t *fine_base = NULL; /* fine table_base */



static inline void create_page_tables(uint8_t *buffer)
{
    uint16_t i = 0;

    tt_base = (uint32_t *)buffer;
    fine_base = tt_base + NUM_TT_ENTRIES;
    cp_base = fine_base + NUM_FINE_ENTRIES;

    for (i = 0; i < NUM_TT_ENTRIES; i++) {
        *(tt_base + i) = CREATE_FAULT_ENTRY;
    }

    for (i = 0; i < NUM_FINE_ENTRIES; i++) {
        *(fine_base + i) = CREATE_FAULT_ENTRY;
    }

    for (i = 0; i < NUM_CP_ENTRIES; i++) {
        *(cp_base + i) = CREATE_FAULT_ENTRY;
    }

    mmu_inited = TRUE;

}


err_t mmu_init(uint8_t *buffer, uint32_t size)
{
    if (size < MMU_MEM_MIN_SIZE) {
        return ERR_NO_MEM;
    }

    if ((uint32_t)buffer & (uint32_t)(MMU_MEM_MIN_ALIGNMENT - 1)) {
        return ERR_NOT_ALIGNED;
    }

    create_page_tables(buffer);
    return ERR_SUCCESS;
}
  
